var api = require('../../config/api.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    toast1Hidden: true,
    modalHidden: true,
    notice_str: '',

    username: '',
    mobile: '',
    adminer: '',
    address: '',

  },


  toast1Change: function(e) {
    this.setData({
      toast1Hidden: true
    });
  },

  //表单提交
  formSubmit: function(e) {
    wx.showModal({
      title: '提示',
      content: '是否确认保存信息',
      confirmColor: "#43CBC9",
      success: (res) => {
        wx.showLoading({
          title: '加载中',
        })
        if (res.confirm) {
          console.log('for发生了submit事件，携带数据为：', e.detail.value);

          var reg = /^[0-9a-zA-Z]+$/;
          var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;

          if (e.detail.value.mobile.length == 0) {
            wx.showToast({
              title: "手机号不能为空!",
              icon: 'none',
              duration: 2000
            });
            return false;
          } else if (e.detail.value.mobile.length != 11) {
            wx.showToast({
              title: "手机号长度不正确!",
              icon: 'none',
              duration: 2000
            });
            return false;
          } else if (!myreg.test(e.detail.value.mobile)) {
            wx.showToast({
              title: '手机号格式有误！',
              icon: 'none',
              duration: 2000
            })
            return false;
          } else if (e.detail.value.address.length == 0) {
            wx.showToast({
              title: "地址不能为空!",
              icon: 'none',
              duration: 2000
            });
            return false;
          }

          wx.request({
            url: api.MineInfoUpdate,
            data: {
              id: wx.getStorageSync('userId'),
              mobile: e.detail.value.mobile,
              address: e.detail.value.address
            },
            method: 'POST',
            header: {
              'Accept': 'application/json',
              'ACCESS-TOKEN': wx.getStorageSync('token')
            },
            success: (res) => {
              wx.showLoading({
                title: '加载中',
              })
              if (res.data.success) {
                wx.showModal({
                  title: '提示',
                  content: res.data.msg,
                  confirmColor: "#43CBC9",
                  showCancel: false,
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                      wx.navigateBack({})
                    }
                  }
                });
                wx.hideLoading();
              }

            }
          })

        } else if (res.cancel) {
          console.log('用户点击取消')
          wx.hideLoading();
        }
      }
    })
  },

  formReset: function() {
    wx.showToast({
      title: "已取消操作!",
      icon: 'none',
      duration: 2000
    });
    wx.navigateBack({});
  },

  //弹出确认框  
  modalTap: function(e) {
    this.setData({
      modalHidden: true
    })
  },
  confirm_one: function(e) {
    console.log(e);
    this.setData({
      modalHidden: true,
      toast1Hidden: false,
      notice_str: '提交成功'
    });
  },
  cancel_one: function(e) {
    console.log(e);
    this.setData({
      modalHidden: true,
      toast1Hidden: false,
      notice_str: '取消成功'
    });
  },
  //弹出提示框  
 

  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(e) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: api.MineInfoGet,
      data: {},
      method: 'GET',
      header: {
        'Accept': 'application/json',
        'ACCESS-TOKEN': wx.getStorageSync('token')
      },
      success: (res) => {
        if (res.data.success) {
          that.setData({
            username: res.data.obj.username,
            mobile: res.data.obj.mobile,
            address: res.data.obj.address,
            adminer: wx.getStorageSync("adminer")
          });
        }
      }

    })

    var admin = wx.getStorageSync("usertype");
    if (admin == 1) {
      that.setData({
        adminer: '代理商',
      })
    } else if (admin == 3) {
      that.setData({
        adminer: '合伙人',
      })
    }


    wx.hideLoading()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})